/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import controller.Action;
import java.io.IOException;
import java.sql.SQLDataException;
import java.sql.SQLException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import model.Conta;
import model.Extrato;
import model.Operacao;
import model.OperacaoAberturarConta;
import model.OperacaoDeposito;
import persistence.ContaDAO;
import persistence.ExtratoDAO;
import persistence.OperacaoDAO;

/**
 *
 * @author Leandro
 */
public class OperacaoDepositoAction implements Action {

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response, Integer contaLogada) throws IIOException {

        String valorDeposito = request.getParameter("valor");
        Double valor = null;

        if (valorDeposito.isEmpty()) {
            try {
                response.sendRedirect("indexErro.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(OperacaoDepositoAction.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            valor = Double.parseDouble(valorDeposito);

            if (valor <= 0) {
                try {
                    request.setAttribute("numeroConta", contaLogada);
                    RequestDispatcher dispatcher = request.getRequestDispatcher("valorInvalido.jsp");
                    dispatcher.forward(request, response);
                } catch (ServletException ex) {
                    Logger.getLogger(OperacaoDepositoAction.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(OperacaoDepositoAction.class.getName()).log(Level.SEVERE, null, ex);
                }

            }else{

            Integer numeroConta = null;
            if (contaLogada.equals("") || contaLogada == null) {
                numeroConta = Integer.parseInt(request.getParameter("numeroConta"));
            } else {
                numeroConta = contaLogada;
            }

            if (valor.equals("") || numeroConta.equals("")) {
                try {
                    response.sendRedirect("erroIndex.xhtml");
                } catch (IOException ex) {
                    Logger.getLogger(OperacaoDepositoAction.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                Conta conta = new Conta();
                conta.setNumero(numeroConta);
                conta.setSaldo(valor);
                try {
                    OperacaoDAO.getInstace().realizaDeposito(conta);

                    Conta contaSalva = ContaDAO.getInstace().recuperaContaPorNumero(numeroConta);

                    Operacao operacao = new Operacao();
                    operacao.setTipoOperacao(new OperacaoDeposito());
                    //  operacao = OperacaoDAO.getInstace().insereOperacao(operacao);
                    //PERSISTE O EXTRATO
                    Extrato extrato = new Extrato();
                    extrato.setConta(contaSalva);
                    extrato.setOperacao(operacao);
                    extrato.setStrOperacao(extrato.getOperacao().getTipoOperacao().getTipoOperacao());


                    GregorianCalendar calendar = new GregorianCalendar();
                    calendar.setTime(new Date());
                    int dia = calendar.get(GregorianCalendar.DAY_OF_MONTH);
                    extrato.setDia(dia);
                    extrato.setSaldo(contaSalva.getSaldo());
                    extrato.setValor(valor);
                    ExtratoDAO.getInstace().insereExtrato(extrato);

                    try {
                        request.setAttribute("numeroConta", numeroConta);
                        RequestDispatcher dispatcher = request.getRequestDispatcher("sucessoDeposito.jsp");
                        dispatcher.forward(request, response);
                    } catch (ServletException ex) {
                        Logger.getLogger(OperacaoDepositoAction.class.getName()).log(Level.SEVERE, null, ex);
                    }

                } catch (SQLDataException ex) {
                } catch (ClassNotFoundException ex) {
                } catch (SQLException ex) {
                } catch (IOException ex) {
                    try {
                        response.sendRedirect("indexErro.xhtml");
                    } catch (IOException ex1) {
                    }
                }

            }
        }
        }
    }
}
